package com.axiu.lesson11_2;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private Button start, stop, playBtn;
    private File sound;
    MediaRecorder recorder;
    TextView time;
    Timer timer;
    MediaPlayer mediaPlayer;
    ProgressBar progressBar;
    TextView temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String path = null;
        try {
            path = Environment.getExternalStorageDirectory().getCanonicalFile()+"/sound.amr";
        } catch (IOException e) {
            e.printStackTrace();
        }
        final String filePath = path;
        timer = new Timer();
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        time = (TextView) findViewById(R.id.time) ;
        playBtn = (Button) findViewById(R.id.playBtn);
        mediaPlayer = new MediaPlayer();
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        temp = (TextView) findViewById(R.id.temp);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    Toast.makeText(MainActivity.this,"SD卡不存在",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    try {
                        displayRecordTime();

                        Log.i("文件存储路径:",filePath);
                        sound = new File(filePath);
                        Log.i("文件AbsolutePath:",sound.getAbsolutePath());
                        recorder = new MediaRecorder();
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        recorder.setOutputFile(sound.getAbsolutePath());
                        recorder.prepare();
                        recorder.start();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recorder.stop();
                recorder.release();
                recorder = null;
                timer.cancel();
            }
        });

        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File file = new File(filePath);
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(file.getAbsolutePath());

                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //
                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    @Override
                    public void run() {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                int i = mediaPlayer.getCurrentPosition()/1000*100/(mediaPlayer.getDuration()/1000);
                                progressBar.setProgress(i);
                                temp.setText(mediaPlayer.getCurrentPosition()/10+"  "+mediaPlayer.getDuration()/1000);
                                if(!mediaPlayer.isPlaying()){
                                    t.cancel();
                                }
                            }
                        });
                    }
                }, 0, 10);
                mediaPlayer.start();

               // temp.setText(+"");
            }
        });

    }

    @Override
    protected void onDestroy() {
        if(sound != null && sound.exists()){
            recorder.stop();
            recorder.release();
            recorder = null;
        }
        super.onDestroy();
    }

    private void displayRecordTime(){

        final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        final long startTime = System.currentTimeMillis();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Date recordTime = new Date(System.currentTimeMillis() - startTime);

                        String text = format.format(recordTime);
                        time.setText(text);
                    }
                });
            }
        }, 1000, 1000);
    }
}
